package com.Servlet;

import com.dao.DaoFactory;
import com.entity.book;
import com.entity.search;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

public class chapter_examine_dealServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setHeader("Cache-control", "no-cache");
        request.setCharacterEncoding("UTF-8");

        String state = request.getParameter("state");
        String bookname = request.getParameter("bookname");
        String author_name = request.getParameter("author_name");
        int chapter_no = Integer.parseInt(request.getParameter("chapter_no"));
        String chapter_name = request.getParameter("chapter_name");
        String address = request.getParameter("address");

        System.out.println("--------------审核章节---------------");
        System.out.println("bookname:" + bookname);
        System.out.println("author_name:" + author_name);
        System.out.println("chapter_name:" + chapter_name);
        System.out.println("chapter_no:" + chapter_no);
        System.out.println("address:" + address);
        System.out.println("state:" + state);
        System.out.println("---------------------------------");

        if (state.equals("0")) {
            System.out.println("未通过");
            try {
                DaoFactory.getInstance().getchapter_examineDao().delBybookname(bookname, chapter_name);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        } else {
            System.out.println("通过");
            book newchapter = new book();
            newchapter.setAddress(address);
            newchapter.setChapter_no(chapter_no);
            newchapter.setChapter_name(chapter_name);
            newchapter.setAuthor_name(author_name);
            newchapter.setBookname(bookname);

            try {
                DaoFactory.getInstance().getchapter_examineDao().delBybookname(bookname,chapter_name);
            } catch (SQLException e) {
                e.printStackTrace();
            }

            try {
                DaoFactory.getInstance().getbookDao().add(newchapter);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        request.getRequestDispatcher("chapter_examine.jsp").forward(request, response);

    }
}

